﻿using System.Collections.Generic;

namespace NodeBasedSkillEditor
{
    public class DAG_Data
    {
        public Dictionary<uint, NodeData> AllNodes = new Dictionary<uint, NodeData>(8);
        /// <summary>key必须为outputPort，value为连接的inputPort的集合</summary>
        public Dictionary<uint, HashSet<uint>> Links = new Dictionary<uint, HashSet<uint>>(8);
        /// <summary>0 表示没有有效的起点</summary>
        public uint EntryNodeId = DAGHelper.InvalidNodeId; 

        public void AddNodeData(NodeData nodeData)
        {
            if (!AllNodes.ContainsKey(nodeData.LocalId))
            {
                AllNodes.Add(nodeData.LocalId, nodeData);
            }
        }

        public void RemoveNodeData(NodeData nodeData)
        {
            AllNodes.Remove(nodeData.LocalId);
        }

        public void AddLinkData(uint fromPort, uint toPort)
        {
            HashSet<uint> toPorts;
            if (!Links.TryGetValue(fromPort, out toPorts))
            {
                toPorts = new HashSet<uint>();
                Links.Add(fromPort, toPorts);
            }
            toPorts.Add(toPort);
        }

        public void RemoveLinkData(uint fromPort, uint toPort)
        {
            HashSet<uint> toPorts;
            if (Links.TryGetValue(fromPort, out toPorts))
            {
                toPorts.Remove(toPort);
                if (toPorts.Count == 0)
                {
                    Links.Remove(fromPort);
                }
            }
        }

        public void Clear()
        {
            AllNodes.Clear();
            Links.Clear();
            EntryNodeId = 0;
        }
    }
}
